package com.silknet.general;

import java.util.TreeMap;

public class Language {
	private static String codes = "97:4304;98:4305;103:4306;100:4307;101:4308;118:4309;122:4310;84:4311;105:4312;107:4313;108:4314;109:4315;110:4316;111:4317;112:4318;74:4319;114:4320;115:4321;116:4322;117:4323;102:4324;113:4325;82:4326;121:4327;83:4328;67:4329;99:4330;90:4331;119:4332;87:4333;120:4334;106:4335;104:4336";

	private static TreeMap<Integer, Integer> tmCodes = null;

	public static String translate(String value) {
		initCodes();
		if (value == null)
			value = "";
		char[] crs = value.toCharArray();
		for (int i = 0; i < crs.length; i++) {
			Integer c = tmCodes.get((int) crs[i]);
			if (c != null)
				crs[i] = (char) c.intValue();
		}
		value = new String(crs);
		return value;
	}

	public static String toHex(String value) {
		initCodes();
		if (value == null)
			value = "";
		char[] crs = value.toCharArray();
		value = "";
		for (int i = 0; i < crs.length; i++) {
			Integer c = tmCodes.get((int) crs[i]);
			if (c != null)
				value += "\\u"+Integer.toHexString(c);
			else
				value += crs[i];
		}

		return value;
	}

	private static void initCodes() {
		if (tmCodes == null) {
			String[] each = codes.split(";");
			TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
			for (String code : each) {
				try {
					if (code.trim().length() == 0)
						continue;
					String[] kevalue = code.split(":");

					int key = Integer.parseInt(kevalue[0]);
					int vvalue = Integer.parseInt(kevalue[1]);
					map.put(key, vvalue);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			tmCodes = map;
		}
	}
	
	public static void main(String[] args) {
		char ch='№';
		System.out.println(Integer.toHexString((int)ch));
		System.out.println(toHex("Z X C V B N M"));
	}
}
